import pymysql
import sys
import json
import ibm_db
import datetime


class alarm:
    def __init__(self, data: dict = None):
        # self.type = data['type']
        # self.host = data['host']
        # self.port = data['port']
        # self.user = data['user']
        # self.password = data['password']
        # self.database = data['database']
        # self.schema = data['schema']
        # self.table = data['table']
        self.type = "db2"
        self.host = "172.26.34.61"
        self.port = "50000"
        self.user = "db2admin"
        self.password = "p@Sec0.db2,"
        self.database = "KGLG1"
        self.schema = "IPLATTEST"
        self.table = "ALARM_DATABASE_OPEARATOR"
        self.info = data['data']
        self.alarm_pos = data['alarm_pos']
        self.alarm_level = data['alarm_level']
        self.alarm_area = data['alarm_area']
        self.db = None
        self.sql = None
        self.name = '预警'

    def mysql_connect(self):
        self.db = pymysql.connect(host=self.host,
                                  user=self.user,
                                  passwd=self.password,
                                  port=int(self.port),
                                  charset='utf8')

    def db2_connect(self):
        datasource = "DATABASE={0};HOSTNAME={1};PORT={2};PROTOCOL=TCPIP;UID={3};PWD={4};".format(self.database,
                                                                                                 self.host,
                                                                                                 self.port,
                                                                                                 self.user,
                                                                                                 self.password)
        self.db = ibm_db.connect(datasource, "", "")

    def create_sql(self):
        if(self.info=='正常'):
            return
        output_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        self.sql = "INSERT INTO {0}.{1} (RECORD_TIME,NAME,RECORD_DATA,ALARM_POS,ALARM_LEVEL,ALARM_AREA) VALUES('{2}','{3}','{4}','{5}','{6}','{7}');".format(self.schema,
                                                                         self.table,
                                                                         output_time,
                                                                         self.name,
                                                                         self.info,
                                                                         self.alarm_pos,
                                                                         self.alarm_level,
                                                                         self.alarm_area)

    def insert(self):
        self.create_sql()
        if self.type == 'mysql':
            self.mysql_connect()
            cursor = self.db.cursor()
            cursor.execute(self.sql)
            self.db.commit()
            self.db.close()
        else:
            self.db2_connect()
            ibm_db.exec_immediate(self.db, self.sql)
            ibm_db.commit(self.db)
            ibm_db.close(self.db)


def main():
    try:
        arg1 = sys.argv[1]
        dict1 = json.loads(arg1.replace("\\", ""))
        alarm_actor = alarm(dict1)
        alarm_actor.insert()
        output = {'result': 'insert success'}
        print(json.dumps(output))
    except Exception as err:
        print(err)


if __name__ == '__main__':
    main()